// 封装数据请求request
const BASE_URL = 'https://wap.17888666.com/eboApi/app'; // 基础地址
const SYSTEM_URL = 'https://wap.17888666.com/eboApi/system'; //文件地址
const defaultHeader = header => {
  const appInstance = getApp();
  let Authorization = appInstance.globalData.loginInfo.status === '1' ? appInstance.globalData.Authorization : '';
  uni.setStorageSync('Authorization', Authorization);
  return { 'content-type': 'application/JSON', ...header, Authorization };
};
const request = function ({ url, fullUrl = false, data = {}, header = {}, method = 'POST', dataType = 'json' }) {
  return new Promise(async (resolve, reject) => {
    let rheader = await defaultHeader(header);
    uni.request({
      url: fullUrl ? url : BASE_URL + url, // 请求url地址
      data, // 请求参数
      header: rheader, // 请求头
      method, // 请求方式
      dataType, // 返回数据类型
      success: res => {
        if (res.statusCode === 401) {
          uni.navigateTo({ url: '/pages/global/authorizePhone' });
        } else {
          resolve(res.data); // 成功处理res.data中的数据
        }
      },
      fail: err => {
        // 失败调用
        uni.hideLoading(); // 关闭请求提示
        uni.showToast({
          // 提示错误信息
          title: err || '请求错误！',
          icon: 'error',
          duration: 2000
        });
        reject(err); // 失败处理err
      }
    });
  });
};
const uploadFile = async params => {
  let rheader = await defaultHeader({});
  return new Promise((resolve, reject) => {
    uni.uploadFile({
      url: SYSTEM_URL + '/oss/upload',
      method: 'POST',
      header: rheader,

      //#ifndef H5
      filePath: params.path,
      name: 'file',
      //#endif

      //#ifdef H5
      files: [
        {
          name: 'file',
          file: params.file,
          uri: params.filePath
        }
      ],
      //#endif
      // formData: params,
      success: res => {
        resolve(JSON.parse(res.data));
      },
      fail: err => {
        uni.hideLoading(); // 关闭请求提示
        uni.showToast({
          // 提示错误信息
          title: err || '请求错误！',
          icon: 'error',
          duration: 2000
        });
        reject(err);
      },
      complete: () => {}
    });
  });
};
module.exports = {
  request,
  BASE_URL,
  uploadFile
};
